package com.cloudcc.mobile.search;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.cloudcc.cloudframe.util.GsonUtil;
import com.cloudcc.mobile.R;
import com.cloudcc.mobile.manager.RunTimeManager;
import com.cloudcc.mobile.manager.UrlManager;
import com.cloudcc.mobile.search.adapter.SearchMoreAdapter;
import com.google.gson.Gson;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.mypage.model.SearchMoreItem;
import com.mypage.model.SearchMoreResultEntity;

import java.util.ArrayList;
import java.util.List;

public class SerachMoreActivity extends Activity {
    private ImageView imageView_back;
    private TextView textView_title;
    private ListView listView_more;
    private String prefix="";
    private String searchKeyWord="";
    private String title = "";
    private List<SearchMoreItem> list;
    private SearchMoreAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_serach_more);
        initView();
        initData();
    }

    private void initData() {
        list =  new ArrayList<>();
        prefix = getIntent().getStringExtra("prefix");
        title= getIntent().getStringExtra("title");
        textView_title.setText(title);
        searchKeyWord = getIntent().getStringExtra("searchKeyWord");
        HttpUtils utils = new HttpUtils();
        RequestParams params = new RequestParams();
        params.addBodyParameter("binding", RunTimeManager.getInstance()
                .getServerBinding());
        params.addBodyParameter("serviceName", "objectSearch");

        params.addBodyParameter("pageNUM", "1");
        params.addBodyParameter("pageSize", "50");
        params.addBodyParameter("searchKeyWord", searchKeyWord);
        params.addBodyParameter("prefix", prefix);
        Log.d("request",
                "链接" + UrlManager.getInterfaceUrl()
                        + GsonUtil.Object2Json(params));
        utils.send(HttpRequest.HttpMethod.POST, UrlManager.getInterfaceUrl(), params,
                new RequestCallBack<String>() {

                    @Override
                    public void onFailure(HttpException arg0, String arg1) {
                        Log.d("qingqiushibai", "Failure。。。"
                                + arg1);
                    }
                    @Override
                    public void onSuccess(ResponseInfo<String> arg0) {
                        String result = arg0.result;
                        Log.d("request", "success。。。"
                                + result);
                        try {
                            Gson gson = new Gson();
                            SearchMoreResultEntity ss = gson.fromJson(result,
                                    SearchMoreResultEntity.class);
//                            for (int i = 0; i < ss.data.size(); i++) {
//                                SearchMoreItem searchMoreItem = new SearchMoreItem();
//                                searchMoreItem.id = ss.data.get(i).id;
//                                searchMoreItem.name = ss.data.get(i).name;
//                                list.add(searchMoreItem);
//                            }
//                            if (adapter == null) {
                                adapter = new SearchMoreAdapter(SerachMoreActivity.this, ss,title);
                                listView_more.setAdapter(adapter);
                                list.clear();
//                            } else {
//                                adapter.changeData(lishilist, listHestroy);
//                                list.clear();
//                            }
                        }catch (Exception ex){
                        }
                    }
                });

    }

    private void initView() {
        imageView_back  = (ImageView) findViewById(R.id.search_more_back);
        textView_title=(TextView) findViewById(R.id.search_more_title1);
        listView_more=(ListView) findViewById(R.id.search_more_list);
        imageView_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }
}
